package de.gameduell.framework.util {
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.getTimer;
	import de.gameduell.framework.util.SyncTimer;	

	/**
	 * @author David Aleksanyan
	 * Implements interpolation for given depth of passedTime Number
	 * 
	 * FIXME - unreviewed class
	 */
	public class InterpolatedSyncTimer extends SyncTimer {

		private var _interpolatedTime:uint;
		private var _poles:Array;
		private var _depth:uint;

		/**
		 * Class constructor
		 */
		public function InterpolatedSyncTimer(timeline:DisplayObject, depth:uint){
			super(timeline);
			_poles = new Array();
			_depth = depth;
		}
		
		/**
		 * @return the passedTime value interpolated at given depth
		 */
		public function get interpolatedTime():int {
			return _interpolatedTime;
		}
	
		/**
		 * @return the interpolation depth value 
		 */
		public function get interpolationDepth():uint {
			return _depth;
		}		

		override protected function enterFrameHandler(event:Event):void {
			var currTime:uint = getTimer();
			_passedTime = currTime - _lastTime;
			_lastTime   = currTime;
			
			if(_poles.length >= _depth) {
				_poles.shift();
			}
			_poles.push(_passedTime);
			
			var totalTime:uint = 0;
			for (var i:uint = 0;i < _poles.length; i++) {
				totalTime += _poles[i];
			}
			_interpolatedTime = totalTime / _poles.length;
			dispatchEvent(new TimerEvent(TimerEvent.TIMER));
		}
	}
}